package classes;

import java.util.SortedSet;
import java.util.TreeSet;

public class Any implements Comparable<Any>{
	private Integer numAny;	
	private SortedSet <Mes>llistaMesos;
	

	public Any(Integer numAny) {
		super();
		this.numAny = numAny;
		this.llistaMesos = new TreeSet <Mes>();
	}
	public Any(Integer numAny,Integer mes) {
		super();
		this.numAny = numAny;
		this.llistaMesos = new TreeSet <Mes>();
		Mes auxMes= new Mes(mes);
		this.llistaMesos.add(auxMes);
	}

	public void afegirMes(Mes mes) {
		
		boolean aux = this.llistaMesos.add(mes);
		
		
	}


	public Integer getNumAny() {
		return numAny;
	}
	
	public Mes getMes(Mes compararMes){
		Mes auxMes=null;
		
		for (Mes mes : llistaMesos){
			if (mes.equals(compararMes)){
				auxMes= mes;
				break;
			}
		}
		
		return auxMes;
	}
	public Mes getMes(int numMes){
		Mes auxMes=null;
		
		for (Mes mes : llistaMesos){
			if (mes.getNumMes()==numMes){
				auxMes= mes;
				break;
			}
		}
		
		return auxMes;
	}

	public SortedSet<Mes> getLlistaMesos() {
		return llistaMesos;
	}


	@Override
	public int compareTo(Any o) {
		int numAComparar= o.getNumAny();
		
		
		if(numAny<numAComparar){
			return -1;
		}else if(numAny>numAComparar){
			return 1;
		}else{	
			return 0;
		}
	}
	
	@Override
	public String toString() {
		String auxString="";
		
		for (Mes mes : llistaMesos){
			
			auxString+="Mes : " + mes.getNumMes() + "\n";
			auxString+= mes.toString();
		}
		
		
		return auxString;
	}
	public void setMes(int mes, Mes plantillaMes) {
		getMes(mes).setMes(plantillaMes);
		
	}
	public void merge(Any auxAfegir) {
		boolean trobat=false;
		for (Mes auxNouAfegir : auxAfegir.llistaMesos){
			trobat=false;
			for (Mes auxOriginal : this.llistaMesos){
			
				if (auxOriginal.equals(auxNouAfegir)){
					auxOriginal.merge(auxNouAfegir);
					trobat=true;
				}
			}
			if (!trobat){
				this.afegirMes(auxNouAfegir);
			}
		}
		
	}
	@Override
	public boolean equals(Object auxAny) {
		
		return(((Any)auxAny).getNumAny().equals(this.getNumAny()));
	}
	
}
